package com.alibaba.bos.action.base;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.hibernate.boot.model.source.internal.hbm.RelationalObjectBinder.RelationalObjectResolutionContext;
import org.hibernate.boot.registry.StandardServiceInitiator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import com.alibaba.bos.domain.base.Standard;
import com.alibaba.bos.service.base.StandardService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;



@Controller
@ParentPackage("json-default")
@Namespace("/")
@Scope("prototype")
public class StandardAction extends ActionSupport implements ModelDriven<Standard> {

    private Standard standard = new Standard();

    @Override
    public Standard getModel() {
        return standard;
    }

    @Autowired
    private StandardService standardService;
    //添加取派标准
    @Action(value = "standard_save", results = {
            @Result(name = "success", type = "redirect", location = "./pages/base/standard.html") })
    public String save() {
        standardService.save(standard);
        return SUCCESS;
    }
    
    //属性驱动封装数据
    private int page;
    private int rows;
    
    public void setPage(int page) {
        this.page = page;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    //取派页面分页
    @Action(value="standard_pageQuery",results={@Result(name="success",type="json")})
    public String pageQuery(){
        Pageable pageable = new PageRequest(page-1, rows);
        Page<Standard> page=standardService.findPageData(pageable);
        
        //底层需要传递的是page跟rows，用map来传递
        Map<String, Object> pages=new HashMap<>();
        pages.put("rows", page.getContent());
        pages.put("total",page.getTotalElements());
        
        //压入值栈
        ActionContext.getContext().getValueStack().push(pages);
        return SUCCESS;
    }
    
    @Action(value="standard_findAll",results={@Result(name="success",type="json")})
    public String findAll(){
      List<Standard> standards= standardService.findAll();
      ActionContext.getContext().getValueStack().push(standards);
        return SUCCESS;
    }
}
